﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;


namespace DingApi.Controllers
{
    [RoutePrefix("api/Paefso")]
    public class PaefsoController : ApiController
    { 
        [HttpPost]
        [Route("PostPaefso_inq_asmat_no")]
        public System.Data.DataSet PostPaefso_inq_asmat_no([FromBody]  JObject obj)
        {
           
            //定义查询反馈的固定反馈内容，
            //盘库数据，若指定库存查询不到，则返回资材编码的查询；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables[0].Columns.Add("COMPANY_CODE");     //备用
            outBlock.Tables[0].Columns.Add("BRANCH_CODE");      //备用
            outBlock.Tables[0].Columns.Add("STOCK_NO");      //备用

            

            outBlock.Tables[0].Columns.Add("ASMAT_NO");
            outBlock.Tables[0].Columns.Add("ASMAT_NAME");
            outBlock.Tables[0].Columns.Add("SPEC");
            outBlock.Tables[0].Columns.Add("UNIT");
            outBlock.Tables[0].Columns.Add("QUANTITY");
            outBlock.Tables[0].Columns.Add("ASSIST_UNIT");
            outBlock.Tables[0].Columns.Add("ASSIST_NUM");

            //ARCHIVE_FLAG 钉钉端标识用
            outBlock.Tables[0].Columns.Add("ARCHIVE_FLAG");

            

           
            string company_code = obj["COMPANY_CODE"] == null ? " " : obj["COMPANY_CODE"].ToString();
            string branch_code = obj["BRANCH_CODE"] == null ? " " : obj["BRANCH_CODE"].ToString();
            string stock_no = obj["STOCK_NO"] == null ? " " : obj["STOCK_NO"].ToString();
            string pa_location_no = obj["PA_LOCATION_NO"] == null ? " " : obj["PA_LOCATION_NO"].ToString();
            string asmat_no = obj["ASMAT_NO"] == null ? " " : obj["ASMAT_NO"].ToString();
            string asmat_name = obj["ASMAT_NAME"] == null ? " " : obj["ASMAT_NAME"].ToString();
            string spec = obj["SPEC"] == null ? " " : obj["SPEC"].ToString();
            string asmat_big_type = obj["ASMAT_BIG_TYPE"] == null ? " " : obj["ASMAT_BIG_TYPE"].ToString();
            string userid = obj["USERID"] == null ? " " : obj["USERID"].ToString();


             string pro_type = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();

             string[] stock_nos = stock_no.Split(',');
            

             stock_no = stock_no.Replace(",", "','");
            

             string sql_str1 = "	select COMPANY_CODE,STOCK_NO,ASMAT_NO, ASMAT_NAME,SPEC,UNIT,ASSIST_UNIT 	";

            sql_str1 += "	, sum(tx.quantity) QUANTITY ,sum(tx.assist_num) ASSIST_NUM from ";
            
             if (pro_type =="P2")
            {
                sql_str1 += "    TPA0006 tx	";  
            }
            else if (pro_type == "P1")
            {
                sql_str1 += "    TPA0002 tx	";  
            }

             //sql_str1 += "	where  COMPANY_CODE='" + company_code + "'	";
             //sql_str1 += "	and STOCK_NO in '" + stock_no + "'	";

            sql_str1 += "	where STOCK_NO in ('" + stock_no + "')	";
            sql_str1 += "	and PA_LOCATION_NO='" + pa_location_no + "'	";
            sql_str1 += "	and ASMAT_NO='" + asmat_no + "'	";
            sql_str1 += "	group by  COMPANY_CODE,STOCK_NO,	ASMAT_NO, ASMAT_NAME,SPEC,UNIT,ASSIST_UNIT ";



            EI.EIInfo outBlock1 = EF.Utility.ExecQuery(sql_str1);

          

            //每次盘库必须保证查询的库存记录为1条数据【若不为一条，应考虑汇总为1条，盘库只考虑数量】
            //EI.EIInfo outBlock1 = EI.EITuxedo.CallService("paef21_inq", inBlock);

            if (outBlock1.Tables[0].Rows.Count>0)
            {
                for (int i = 0; i < outBlock1.Tables[0].Rows.Count;i++ )
                {
                    outBlock.Tables[0].Rows.Add();
                    outBlock.Tables[0].Rows[i]["ASMAT_NO"] = outBlock1.Tables[0].Rows[i]["ASMAT_NO"];
                    outBlock.Tables[0].Rows[i]["ASMAT_NAME"] = outBlock1.Tables[0].Rows[i]["ASMAT_NAME"];
                    outBlock.Tables[0].Rows[i]["SPEC"] = outBlock1.Tables[0].Rows[i]["SPEC"];

                    outBlock.Tables[0].Rows[i]["UNIT"] = outBlock1.Tables[0].Rows[i]["UNIT"];
                    outBlock.Tables[0].Rows[i]["QUANTITY"] = outBlock1.Tables[0].Rows[i]["QUANTITY"];
                    outBlock.Tables[0].Rows[i]["ASSIST_UNIT"] = outBlock1.Tables[0].Rows[i]["ASSIST_UNIT"];
                    outBlock.Tables[0].Rows[i]["ASSIST_NUM"] = outBlock1.Tables[0].Rows[i]["ASSIST_NUM"];
                    
                    outBlock.Tables[0].Rows[i]["COMPANY_CODE"] = outBlock1.Tables[0].Rows[i]["COMPANY_CODE"];
                    outBlock.Tables[0].Rows[i]["STOCK_NO"] = outBlock1.Tables[0].Rows[i]["STOCK_NO"];

                     
                    //ARCHIVE_FLAG 钉钉端标识用
                    outBlock.Tables[0].Rows[i]["ARCHIVE_FLAG"] = "0";
                }
            }
            else
            {

                EI.EIInfo inBlock = new EI.EIInfo(); 
                inBlock.Tables[0].Columns.Add("ASMAT_NO"); 
                inBlock.Tables[0].Rows.Add();
                inBlock.sys_info.company_code = "LT";
                inBlock.sys_info.userid = userid; 
                inBlock.Tables[0].Rows[0]["ASMAT_NO"] = asmat_no; 
                //返回资材编码的相关数据
                EI.EIInfo outBlock2 = EI.EITuxedo.CallService("pabm11_inq", inBlock);
                if (outBlock2.Tables[0].Rows.Count > 0)
                {
                    //var stock_nos = response[0].Split(' ');
                    for (int i = 0; i < stock_nos.Length;i++ )
                    {
                        outBlock.Tables[0].Rows.Add();
                        outBlock.Tables[0].Rows[i]["ASMAT_NO"] = outBlock2.Tables[0].Rows[0]["ASMAT_NO"];
                        outBlock.Tables[0].Rows[i]["ASMAT_NAME"] = outBlock2.Tables[0].Rows[0]["ASMAT_NAME"];
                        outBlock.Tables[0].Rows[i]["SPEC"] = outBlock2.Tables[0].Rows[0]["SPEC"];

                        outBlock.Tables[0].Rows[i]["UNIT"] = outBlock2.Tables[0].Rows[0]["UNIT"];
                        outBlock.Tables[0].Rows[i]["QUANTITY"] = 0;
                        outBlock.Tables[0].Rows[i]["ASSIST_UNIT"] = outBlock2.Tables[0].Rows[0]["ASSIST_UNIT"];
                        outBlock.Tables[0].Rows[i]["ASSIST_NUM"] = 0;
                        //ARCHIVE_FLAG 钉钉端标识用
                        outBlock.Tables[0].Rows[i]["ARCHIVE_FLAG"] = "0";

                        outBlock.Tables[0].Rows[i]["COMPANY_CODE"] = "";//前台处理
                        outBlock.Tables[0].Rows[i]["STOCK_NO"] = stock_nos[i];

                    }
                   
                }
            }
             
            outBlock.Tables[0].TableName = "Table0";

             

            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }
         
        

        //入库时，显示该授权下的仓库
        [HttpPost]
        [Route("PostPaefso_Load")]
        public System.Data.DataSet PostPaefso_Load([FromBody]  JObject obj)
        { 
            string userid   = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string pro_type = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString(); 



            string sql_str1 =  " select  t.STOCK_NO CODE ";
            sql_str1 += " ,t.stock_name   CODE_DESC_1_CONTENT  ,t.COMPANY_CODE ,t.BRANCH_CODE ";
            sql_str1 += "   from tsimm01 t,tsimm04 tt  ";
            sql_str1 += "    WHERE t.stock_no = tt.stock_no ";
            sql_str1 += "   and tt.staff_id = '" + userid + "' ";

            if (pro_type =="P2")
            {
                sql_str1 += "   and  t.STOCK_TYPE='A'"; 
            }
            else if (pro_type == "P1")
            {
                sql_str1 += "   and  t.STOCK_TYPE='7'";
            }
            else //
            {
                sql_str1 += "   and  t.STOCK_TYPE='A'"; 
            }



            sql_str1 += "   order by t.stock_name  ";



            EI.EIInfo outBlock = EF.Utility.ExecQuery(sql_str1); 

            outBlock.Tables[0].TableName = "STOCK_NO";


            //入库类别
            EI.EIInfo outBlock2 = EF.Utility.GetCodeClassValue("DM41");

            //DataRow dr4 = outBlock.Tables["FIBR"].NewRow();
            //dr4["CODE"] = " ";
            //dr4["CODE_DESC_1_CONTENT"] = " ";
            //outBlock.Tables["FIBR"].Rows.InsertAt(dr4, 0);
            outBlock2.Tables[0].TableName = "DM41";


            outBlock.Tables.Add(outBlock2.Tables[0].Copy());


            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;


        }


        //入库，入库实绩新增 + 入库确认。[json表结构体参数相互传送]
        [HttpPost]
        [Route("PostPaefso_save")] 
        
        public System.Data.DataSet PostPaefso_save([FromBody]  JObject obj)
        {
            //api 只做数据的处理，数据效验应放在后台C++ 或钉钉前端；

            //obj对象区分大小写的；报错。
             
            string USERID   = obj["USERID"]     == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"]   == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"]   == null ? " " : obj["FORMNAME"].ToString();
            string pro_type = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString(); 


            DataTable table_rec = JsonConvert.DeserializeObject<DataTable>(obj["table_rec"].ToString(), new DingApi.ReadAheadDataTableConverter());;
                       
            EI.EIInfo inBlock2 = new EI.EIInfo();
            inBlock2.sys_info.company_code = "LT";
            inBlock2.sys_info.company_name = "龙腾特钢";

            inBlock2.sys_info.userid    = USERID;
            inBlock2.sys_info.username  = USERNAME;
            inBlock2.sys_info.formname  = FORMNAME;


            inBlock2.Tables[0].Merge(table_rec);

            

            inBlock2.Tables[0].TableName = "INS";

            string svr = "";
            if (pro_type == "P2")
            {
                svr += "pmac00_scan_stock3"; //二级库
            }
            else if (pro_type == "P1")
            {
                svr += "pmac00_scan_stock2"; //一级库
            }



            EI.EIInfo outBlock = EI.EITuxedo.CallService(svr, inBlock2);
                     
            //系统信息；
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");
            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);
            return outBlock;

        }

    }
}